<?php namespace Common\Model;
use Hdphp\Model\Model;
//商品管理模型
class Goods extends Model{
	//指定表名
	protected $table = "goods";
	//自动验证
	protected $validate = array(
		array('gname','required','商品名不能为空',3,3),
		array('unit','required','商品单位不能为空',3,3),
		array('marketprice','required','市场价不能为空',3,3),
		array('shopprice','required','商城价不能为空',3,3),
		array('stock','required','总库存不能为空',3,3),
		array('list_pic','required','列表图不能为空',3,3),
		array('click','required','点击率不能为空',3,3),
		
		array('detail_pic','required','商品图册不能为空',3,3),
		array('detail_desc','required','商品详情不能为空',3,3),
		array('detail_service','required','售后服务不能为空',3,3),
	);

	//添加方法
	public function store(){
		//验证未通过
		if(!$this->create()) return false;
		//用本地时间
		date_default_timezone_set('PRC');
		$shelves_time = time();
		$gnumber = mt_rand(100000,999999);
		$gname = Q('post.gname');$unit = Q('post.unit');$marketprice = Q('post.marketprice');
		$shopprice = Q('post.shopprice');$stock = Q('post.stock');	$click = Q('post.click');
		$list_pic = Q('post.list_pic');	$category_cid = Q('post.category_cid');
		$type_tid = Q('post.type_tid');	$adminuser_aid = Q('post.adminuser_aid');
		$brand_bid = Q('post.brand_bid');
		//不写成'{$gname}'
		$gid = Db::table('goods')->insertGetId(['gname'=>$gname,'gnumber'=>$gnumber,'unit'=>$unit,'marketprice'=>$marketprice,'shopprice'=>$shopprice,'stock'=>$stock,'click'=>$click,'shelves_time'=>$shelves_time,'list_pic'=>$list_pic,'category_cid'=>$category_cid,'type_tid'=>$type_tid,'adminuser_aid'=>$adminuser_aid,'brand_bid'=>$brand_bid]);

		$detail_pic = implode('|',Q('post.detail_pic'));
		$detail_desc = Q('post.detail_desc');
		$detail_service = Q('post.detail_service');
		Db::table('detail')->insert(['detail_pic'=>$detail_pic,'detail_desc'=>$detail_desc,'detail_service'=>$detail_service,'goods_gid'=>$gid]);
		
		$GAttrModel = new \Common\Model\GoodsAttr;
		//属性
		$arr = Q('post.arr');
		if(!empty($arr)){
			foreach($arr as $k=>$v){
				//组合数组
				$data = array(
					'gavalue' =>$v,
					'typeattr_taid'=>$k,
					'goods_gid' => $gid
				);
				if(!$GAttrModel->add($data)){
					$this->error = $GAttrModel->getError();
					return false;
				}
			}
		}
		//规格
		$spec = Q('post.spec');
		 if(!empty($spec)){
		 	foreach($spec as $k=>$v){
		 		for($i=0;$i<count($v['value']);++$i){
		 			$data = array(
						'gavalue' => $v['value'][$i],
						'add_price' => $v['add_price'][$i],
						'typeattr_taid' => $k,
						'goods_gid' => $gid
					);
					if(!$GAttrModel->add($data)){
						$this->error = $GAttrModel->getError();
						return false;
					}
		 		}
		 	}
		 }	 
		 return true;
		
	}
	//编辑方法
	public function edit(){
		//验证未通过
		if(!$this->create()) return false;
		//1.得到商品ID
		$gid = Q('get.gid',0,'intval');
		//p(Q('post.'));exit;
		
		//用本地时间
		date_default_timezone_set('PRC');
		$shelves_time = time();
		$gnumber = mt_rand(100000,999999);
		$gname = Q('post.gname');$unit = Q('post.unit');$marketprice = Q('post.marketprice');
		$shopprice = Q('post.shopprice');$stock = Q('post.stock');	$click = Q('post.click');
		$list_pic = Q('post.list_pic');	$category_cid = Q('post.category_cid');
		$type_tid = Q('post.type_tid');	$adminuser_aid = Q('post.adminuser_aid');
		$brand_bid = Q('post.brand_bid');
		//不写成'{$gname}'
		Db::table('goods')->where("gid",$gid)->update(['gname'=>$gname,'gnumber'=>$gnumber,'unit'=>$unit,'marketprice'=>$marketprice,'shopprice'=>$shopprice,'stock'=>$stock,'click'=>$click,'shelves_time'=>$shelves_time,'list_pic'=>$list_pic,'category_cid'=>$category_cid,'type_tid'=>$type_tid,'adminuser_aid'=>$adminuser_aid,'brand_bid'=>$brand_bid]);
		
		
		$detail_picOld =  Q('post.detail_picOld');	
		if($detail_picOld != NULL){
			$detail_picOld= implode('|', $detail_picOld);
		}
		$detail_pic =  Q('post.detail_pic');
		if($detail_pic != NULL){
			$detail_pic = implode('|', $detail_pic);
		}		
		if($detail_picOld == NULL){
			$pic_all = $detail_pic;
		}elseif($detail_pic == NULL){
			$pic_all = 	$detail_picOld;
		}else{
			$pic_all = 	$detail_picOld .'|'.$detail_pic;
		}		
		$detail_desc =  Q('post.detail_desc');
		$detail_service =  Q('post.detail_service');
		Db::table('detail')->where("goods_gid",$gid)->update(['detail_pic'=>$pic_all,'detail_desc'=>$detail_desc,'detail_service'=>$detail_service]);
		
		
		$arr=$_POST;
		//p($arr);exit;
		$GoodsAttrModel= new \Common\Model\GoodsAttr;				
	   $GoodsAttrModel->where("goods_gid={$gid}")->delete();
		//规格			
		foreach ($arr['spec'] as $k => $v) {
			foreach ($v['value'] as $ke => $va) {			
			$data = array(
				'typeattr_taid' => $k,
				'gavalue' =>$va,
				'add_price'=>$v['add_price'][$ke],
				'goods_gid'=>$gid			
			);
			$GoodsAttrModel->add($data);							
			}			
		}
		//属性	
		if(!empty($arr['attr'])){
			foreach ($arr['attr'] as $k => $v) {						
				$data = array(
					'typeattr_taid' => $k,
					'gavalue' =>$v,
					'add_price'=>'0',
					'goods_gid'=>$gid			
				);
				$GoodsAttrModel->add($data);		
			}	
		}		
		return true;
		
		
		
	}
} 
	
?>